
#include "mainwindow.h"
#include "./ui_mainwindow.h"

#include "QPainter"
#include "QMouseEvent"
#include "math.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //设置窗口大小
    setFixedSize(margin * 2 + block_size * board_size,
                 margin * 2 + block_size * board_size);

    initGame();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    //绘制棋盘
    painter.setRenderHint(QPainter::Antialiasing,true);//抗锯齿
    for(int i=0;i<board_size+1;i++){
        //竖线
        painter.drawLine(margin+block_size*i,margin,
                         margin+block_size*i,size().height()-margin);
        //横线
        painter.drawLine(margin,margin+block_size*i,
                         size().width()-margin,margin+block_size*i);
    }

    //绘制选中点
    QBrush brush;
    brush.setStyle(Qt::SolidPattern);
    //绘制落点标记（防止鼠标出框越界）
    if(clickRow>0 && clickRow<board_size &&
        clickCol>0 && clickCol<board_size &&
        game->gameMapVec[clickRow][clickCol]==0)
    {
        if(game->playerflag)
            brush.setColor(Qt::black);
        else
            brush.setColor(Qt::white);
        painter.setBrush(brush);
        painter.drawRect(margin+block_size*clickCol-mark_size/2,margin+block_size*clickRow-mark_size/2,mark_size,mark_size);
    }

    //绘制棋子
    for(int i=0;i<board_size;i++)
    {
        for(int j=0;j<board_size;j++)
        {
            if(game->gameMapVec[i][j]==1)
            {
                brush.setColor(Qt::black);
                painter.setBrush(brush);
                painter.drawEllipse(margin+block_size*j-chess_radius,margin+block_size*i-chess_radius,chess_radius*2,chess_radius*2);
            }
            else if(game->gameMapVec[i][j]==-1)
            {
                brush.setColor(Qt::white);
                painter.setBrush(brush);
                painter.drawEllipse(margin+block_size*j-chess_radius,margin+block_size*i-chess_radius,chess_radius*2,chess_radius*2);
            }
        }
    }

}

void MainWindow::initGame()
{
    //初始化游戏模型
    game=new GameModel;
    initAIGame();
}

void MainWindow::initAIGame()
{
    game_type=AI;
    game->gameStatus=Playing;

    //在数据模型中进行初始化功能
    game->startGame(game_type);
    update();
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    //通过鼠标确定落子的标记
    int x=event->x();
    int y=event->y();

    //棋盘边缘不能落子(保证每个点周围有八个点,方便操作）
    if(x>=margin+block_size/2 &&
        x<size().width()-margin-block_size/2 &&
        y>=margin+block_size/2 &&
        y<size().height()-margin-margin-block_size/2)
    {
        //获取左上角的点
        int col=(x-margin)/block_size;//横坐标
        int row=(y-margin)/block_size;//纵坐标
        int leftTopX=margin+block_size*col;
        int leftTopY=margin+block_size*row;

        clickRow = -1;//初始化
        clickCol = -1;
        int len=0;
        select=false;

        //计算距离
        len=sqrt((x-leftTopX)*(x-leftTopX)+(y-leftTopY)*(y-leftTopY));//与左上角
        if(len<pos_offset)
        {
            clickRow=row;
            clickCol=col;
            if(game->gameMapVec[clickRow][clickCol]==0){
                select=true;
            }
        }
        len=sqrt((x-leftTopX-block_size)*(x-leftTopX-block_size)+(y-leftTopY)*(y-leftTopY));//与右上角
        if(len<pos_offset)
        {
            clickRow=row;
            clickCol=col+1;
            if(game->gameMapVec[clickRow][clickCol]==0){
                select=true;
            }
        }
        len=sqrt((x-leftTopX)*(x-leftTopX)+(y-leftTopY-block_size)*(y-leftTopY-block_size));//与左下角
        if(len<pos_offset)
        {
            clickRow=row+1;
            clickCol=col;
            if(game->gameMapVec[clickRow][clickCol]==0){
                select=true;
            }
        }
        len=sqrt((x-leftTopX-block_size)*(x-leftTopX-block_size)+(y-leftTopY-block_size)*(y-leftTopY-block_size));//与右下角
        if(len<pos_offset)
        {
            clickRow=row+1;
            clickCol=col+1;
            if(game->gameMapVec[clickRow][clickCol]==0){
                select=true;
            }
        }
    }
    //重绘
    update();//更新
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if(select==false){
        return;
    }
    //落子后
    else{
        select=false;
    }

    chessPerson();
}

void MainWindow::chessPerson()
{
    //合法位置
    if(clickRow!=-1  && clickCol!=-1 && game->gameMapVec[clickRow][clickCol]==0)
    {
        //落子
        game->actionPerson(clickRow,clickCol);

        update();
    }
}
